package com.example.administrator.xxnapp.fragment.picture;

import com.example.administrator.xxnapp.bean.PictureBean;
import com.example.administrator.xxnapp.http.HttpUrl;
import com.example.administrator.xxnapp.http.ResultStringCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Administrator on 2018/4/23.
 */

public class PicturePresenter {
    private static final String TAG = "PicturePresenter";
    private IViewPicture mIView;
    private IModelPictrue mIModel;

    public PicturePresenter(IViewPicture mIView, IModelPictrue mIModel) {
        this.mIView = mIView;
        this.mIModel = mIModel;
    }

    public void getPicture(int userId){
        mIModel.getPictures(userId)
                .execute(new ResultStringCallback() {

                    @Override
                    public void onResultSuccess(Gson gson, String dataJsonStr) {
                        Type type = new TypeToken<List<PictureBean>>() {}.getType();
                        List<PictureBean> pictureBeans= gson.fromJson(dataJsonStr,type);
                        mIView.getPictureSuccess(pictureBeans);
                    }

                    @Override
                    public void onResultOther(int result, String message) {
                        mIView.getPictureError(result,message);
                    }
                });
    }

    public void destroy(){
        if (mIView != null){
            mIView = null;
        }
    }

    private String files(File file){
        String lineStr = "--";
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String readline = "";
            StringBuffer sb = new StringBuffer();
            while ((readline = br.readLine()) != null) {
                System.out.println("readline:" + readline);
                sb.append(readline);
            }
            br.close();
            lineStr = sb.toString();
//            System.out.println("读取成功：" + sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lineStr;
    }
}
